package lg.minecraft.plugin.library.listener;

import lg.minecraft.plugin.library.LGLibrary;
import lg.minecraft.plugin.library.input.InputType;
import lg.minecraft.plugin.library.input.PlayerInputManager;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;

/**
 * @author langle__
 * @version 1.0
 */
public class PlayerEvent implements Listener {

    @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
    public void onAsyncPlayerChat(AsyncPlayerChatEvent event) {
        Player player = event.getPlayer();
        String message = event.getMessage();

        if (PlayerInputManager.isWaitingForInput(player, InputType.CHAT)) {
            event.setCancelled(true);
            Bukkit.getScheduler().runTask(LGLibrary.getInstance(), () -> PlayerInputManager.handleInput(player, message));
        }
    }

}
